import {Component} from "@angular/core";
import {FormControl} from "@angular/forms";
import {STRINGS} from "../../../shared/strings";
import "rxjs/add/operator/startWith"
import "rxjs/add/operator/map"
import "rxjs/add/operator/throttleTime"

@Component({
  selector: 'cs-autocomplete',
  templateUrl: './autocomplete.component.html'
})
export class AutocompleteComponent {
  stateCtrl: FormControl
  filteredStates: any
  states = STRINGS

  constructor() {
    this.stateCtrl = new FormControl()
    this.filteredStates = this.stateCtrl.valueChanges
      .startWith(null)
      .throttleTime(200)
      .map(name_ => this.filterStates(name_))
  }

  filterStates(val: string) {
    return val ? this.states.filter((s) => new RegExp(val, 'gi').test(s)) : this.states;
  }

  displayFn(a) {
    console.log(a)
    return a ? a.toUpperCase() : a
  }
}
